/**
 * 
 */
package com.wangh.db.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.wangh.db.desc.DBColumn;


/**
 * @author wang
 *
 */
public class DBUtils {
	
	private static Connection conn;
	
	public static void getConnection(){
		try {
			Class.forName("org.h2.Driver");
			conn = DriverManager.getConnection("jdbc:h2:csvdb");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void createTable(String name, List<DBColumn>  columns){
		String desc = null;
		
		for(int i=0; i<columns.size(); i++){
			desc = desc + "\"" + columns.get(i).getType().toUpperCase() + " " + "(" + columns.get(i).getLength() + ")" + "\"";
			if(i != columns.size() - 1){
				desc = desc + ",";
			}
		}
		
		try {
			Statement stat = conn.createStatement();
			stat.execute("CREATE TABLE" + " " + name.toUpperCase() + desc);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
